package main

import (
	"net/http"

	"git.oschina.net/antlinker/antevent/consapi"
	"git.oschina.net/antlinker/antevent/server/evtlog"
	"github.com/antlinker/alog"
	"github.com/antlinker/alog/log"
	"gopkg.in/urfave/cli.v2"
)

func createStart() []cli.Flag {
	//jobid string, gorunCnt int, loadSpaceTime time.Duration, maxerrcnt
	fs := []cli.Flag{
		&cli.BoolFlag{
			Name:    "debug",
			Aliases: []string{"d"},
			Usage:   "是否开启日志，默认关闭",
		},
		&cli.StringFlag{
			Name:    "listen",
			Aliases: []string{"l"},
			Value:   ":17731",
			Usage:   "监听ip:port",
		},
		&cli.StringFlag{
			Name:    "path",
			Aliases: []string{"p"},
			Value:   "/test",
			Usage:   "监听path",
		},
		&cli.StringFlag{
			Name:    "pprof",
			Aliases: []string{"pp"},
			Value:   "",
			Usage:   "启动pprof分析程序,默认不启动，可以设置:9090,指定一个监听ip端口通过http访问",
		},
	}
	return fs
}
func initcons(c *cli.Context) error {
	debug := c.Bool("debug")
	if debug {
		alog.GALog.SetLogLevel(log.DEBUG)
	} else {
		alog.GALog.SetLogLevel(log.INFO)
	}
	pprof(c)
	l := c.String("listen")
	p := c.String("path")
	handler, recv := consapi.CreateHandler(10)
	http.Handle(p, handler)
	t := createTotaler(true)
	go func(recv <-chan *consapi.Event) {
		for e := range recv {
			t.AddEvent()
			evtlog.Debug("id:%s e:%s a:%s s:%s p:%v t:%v\n", e.Eid, e.Etype, e.Action, e.From, e.Data, e.OccTime)
		}
	}(recv)
	evtlog.Info("启动服务:%s%s", l, p)
	return http.ListenAndServe(l, nil)
}
